package com.code.leetcode.before50;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/24 8:52
 */
public class The69 {
    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        int rtn = -1;
        while (left <= right) {
            int mid = left + (right - 1) / 2;
            if ((long) mid * mid <= x) {
                rtn = mid;
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return rtn;
    }

    public int mySqrtToMe(int x) {
        int left = 0;
        int right = x;
        int rtn = -1;
        while (left <= right) {
            int mid = left + (right - left) >> 1;
            if (mid > x / mid) {
                right = mid - 1;
            } else if (mid  < x / mid) {
                rtn = mid;
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return rtn;
    }

}
